/**
 *Description: solaris平台,网络自我发现程序
 *程序返回：网络接口的物理接口名
 */

package com.sinodata.bsm.cicp.finder.solaris;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class Net extends SolarisFinder {
    private static final Logger logger = Logger.getLogger(Net.class);
    private static final String SHELL = "netstat -i";

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] netRes = null;
        BufferedReader reader = null;
        String strLine = null;
        try {
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            while ((strLine = reader.readLine()) != null && !strLine.trim().equals("")) {
                StringTokenizer st = new StringTokenizer(strLine);
                String name = st.nextToken();
                if (!name.equals("Name") && !name.endsWith("*") && !name.trim().equals("")) {
                    netRes = new String[4];
                    list.add(netRes);
                    netRes[0] = name;
                    netRes[1] = name;
                    netRes[2] = getHostName(ip) + " net interface " + name;
                    netRes[3] = name; //物理接口名
                }
            }
            return list;
        } catch (IOException e) {
            logger.error("failed to find Solaris Net", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }
}
